import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutterhouse/api/index.dart';
import 'package:flutterhouse/pages/Home/components/home_list.dart';
import 'package:flutterhouse/pages/Home/components/home_nav.dart';
import 'package:flutterhouse/utils/TokenManager.dart';
import 'package:flutterhouse/utils/toast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List _announceList = [];
// 获取数据
  void initState() {
    super.initState();
    getAnnounceList();
  }

  // 获取公共列表
  getAnnounceList() async {
    try {
      final res = await getAnnounceListAPI();
      // 封装的轻提示工具类
      PromptAction.showSuccess('获取数据成功');
      // 普通弹窗
      // showDialog(
      //   context: context,
      //   builder: (context) {
      //     return AlertDialog(
      //       title: const Text('公告列表'),
      //       content: Text(res.toString()),
      //     );
      //   },
      // );
      setState(() {
        _announceList = res;
      });
    } catch (e) {
      PromptAction.showError('获取数据失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: const Text('享+社区'),
      ),
      body: ListView(
        children: [
          testToken(),
          SizedBox(height: 10),
          testTimer(),
          // 导航组件
          const HomeNav(),
          // 轮播图
          Image.asset(
            'assets/images/banner@2x.jpg',
            width: double.infinity,
            height: 200,
          ),
          // 列表组件
          HomeList(
            list: _announceList,
          )
        ],
      ),
    );
  }

  int _count = 5;
  Timer? _timer;
// 测试倒计时
  testTimer() {
    return Row(
      children: [
        ElevatedButton(
          onPressed: () {
            if (_timer != null) {
              return;
            }
            _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
              setState(() {
                _count--;
              });
            });
          },
          child: const Text('开始倒计时'),
        ),
        Text('  $_count s  '),
        ElevatedButton(
          onPressed: () {
            _timer?.cancel();
            _timer = null;
          },
          child: const Text('结束倒计时'),
        )
      ],
    );
  }

// 测试token的存储获取和删除
  testToken() {
    return Row(
      children: [
        ElevatedButton(
            onPressed: () {
              tokenManager.setToken('123456');
            },
            child: const Text('设置token')),
        ElevatedButton(
            onPressed: () async {
              final token = await tokenManager.getToken();
              PromptAction.showSuccess('token: $token');
            },
            child: const Text('获取token')),
        ElevatedButton(
            onPressed: () async {
              await tokenManager.removeToken();
              PromptAction.showSuccess('删除token成功');
            },
            child: const Text('删除token'))
      ],
    );
  }
}
